package com.zh.mediaservice.controller.User;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConstants;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.easysdk.factory.Factory;
import com.zh.mediacommon.constant.AliPayConstant;
import com.zh.mediacommon.result.Result;
import com.zh.mediapojo.dto.OrderDTO;
import com.zh.mediapojo.entity.Order;
import com.zh.mediapojo.vo.AlipayVO;
import com.zh.mediaservice.config.AliPayConfig;
import com.zh.mediaservice.mapper.OrderMapper;
import com.zh.mediaservice.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/alipay")
public class AliPayController {
    private static final Logger log = LoggerFactory.getLogger(AliPayController.class);
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/pay")
    public Result pay(@RequestBody OrderDTO orderDto) {
        log.info("订单信息{}",orderDto);
        orderService.adjustConcurrency(orderDto.getDoctorId(),orderDto.getReserveTime());
        AlipayVO alipayVO = orderService.createOrder(orderDto);
        return Result.success(alipayVO);
    }

    @PostMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request){
        orderService.notifyOrder(request);
        return "success";
    }
    @GetMapping("/success")
    public Result paySuccess(@RequestParam String outTradeNo) {
        orderService.selectOrderStatus(outTradeNo);
        return Result.success("订单支付完成");
    }
    @GetMapping("/payNotPayOrder/{id}")
    public Result payNotPayOrder(@PathVariable Long id) {
        AlipayVO alipayVO = orderService.payNotPayOrder(id);
        return Result.success(alipayVO);
    }
}
